package com.example.demo;

import org.assertj.core.util.Sets;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * @author xian.wang
 * @since 下午10:10 2021/12/18
 */
public class ScriptDemo {

    public static void main(String[] args) throws Exception{
        // 2,3,4,5
        Set<Integer> pageSet = Sets.newHashSet();
        pageSet.add(2);
        pageSet.add(3);
        pageSet.add(4);
        pageSet.add(5);
        for (Integer pageNum : pageSet) {
            onePage(pageNum);
        }


    }

    private static void onePage(int pageNum) {
        String body = HttpRequest.get("https://developer.aliyun.com/developer/api/download/getDownloadTopList?pageSize=20&pageNum="+ pageNum +"&category=total")
                .execute().body();
        String downUrl = "wget 'https://developer.aliyun.com/topic/download?spm=a2c6h.20593702.0.0.46d87a8e0mXYn2&id=%s' -O '%s';";
        JSONObject jsonObject = JSONUtil.parseObj(body);
        List<Map> resultList = JSONUtil.toList(jsonObject.getByPath("data.list").toString(),Map.class);
        for (Map map : resultList) {
            Map<String,Object> item = ((Map<String, Object>) map);
            Object title = item.get("title");
            Object downloadFileId = item.get("downloadFileId");
            System.out.println(String.format(downUrl, downloadFileId.toString(), title.toString()));
        }
    }
}
